package cn.com.shopec.erp.product.dao.impl;

import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.transform.Transformers;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.stereotype.Repository;

import cn.com.shopec.erp.common.orm.impl.HibernateEntityDao;
import cn.com.shopec.erp.common.page.PageFinder;
import cn.com.shopec.erp.common.page.PageQuery;
import cn.com.shopec.erp.common.utils.StringUtil;
import cn.com.shopec.erp.product.dao.INventoryCheckDao;
import cn.com.shopec.erp.product.entity.InventoryCheck;

/**
 * 库存盘点dao展现层
 * @author Jack
 *
 */
@Repository
public class InventoryCheckDaoImpl extends HibernateEntityDao<InventoryCheck> implements INventoryCheckDao {

	/**
	 * 库存盘点记录
	 */
	@Override
	public PageFinder<InventoryCheck> getPagedInventoryCheckList(InventoryCheck inventoryCheck, PageQuery pageQuery) {
		if(inventoryCheck!=null){
	    StringBuffer hql = new  StringBuffer("select count(*) from TBL_INVENTORY_CHECK t where deleteFlage = 1 ");
		StringBuffer sql = new  StringBuffer("SELECT "
				+ "t.id as id,"
				+ "t.pId as pId,"
				+ "t.whNo as whNo,"
				+ "t.supNo as supNo,"
				+ "t.prodNo as prodNo,"
				+ "t.commoNo as commoNo,"
				+ "t.productTotal as productTotal,"
				+ "t.qualifiedQuantity as qualifiedQuantity,"
				+ "t.createTime as createTime ,"
				+ "t.deleteFlage as deleteFlage,"
				+ "t.sysProduct as sysProduct,"
				+ "t.yieldProduct as yieldProduct,"
				+ "t.sysYieldProduct as sysYieldProduct,"
				+ "t.defectProduct as defectProduct,"
				+ "t.sysDefectProduct as sysDefectProduct,"
				+ "t.isSysData as isSysData "
				+ "from TBL_INVENTORY_CHECK t  where deleteFlage = 1 ");
		 if(!StringUtil.isBlank(inventoryCheck.getpId())){
				String aql = " and t.pId = '"+inventoryCheck.getpId()+"' ";
				sql.append(aql);
				hql.append(aql);
		 } 
		 
		 if(!StringUtil.isBlank(inventoryCheck.getWhNo())){
				String bql = " and t.whNo  like '%"+inventoryCheck.getWhNo()+"%' ";
				sql.append(bql);
				hql.append(bql);
		 } 
		 
		 if(!StringUtil.isBlank(inventoryCheck.getSupNo())){
				String cql = " and t.supNo like '%"+inventoryCheck.getSupNo()+"%' ";
				sql.append(cql);
				hql.append(cql);
		 } 
		 
		Query query= getSession().createSQLQuery(sql.toString())
				.addScalar("id",StandardBasicTypes.STRING)
				.addScalar("pId",StandardBasicTypes.STRING)
				.addScalar("whNo",StandardBasicTypes.STRING)
				.addScalar("supNo",StandardBasicTypes.STRING)
				.addScalar("prodNo",StandardBasicTypes.STRING)
				.addScalar("commoNo",StandardBasicTypes.STRING)
				.addScalar("productTotal",StandardBasicTypes.LONG)
				.addScalar("qualifiedQuantity",StandardBasicTypes.LONG)
				.addScalar("createTime",StandardBasicTypes.STRING)
				.addScalar("deleteFlage",StandardBasicTypes.INTEGER);
		BigInteger count = (BigInteger) this.createSQLQuery(hql.toString()).list().get(0);
		List list = getSession().createSQLQuery(sql.toString()).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP)
				.setFirstResult((pageQuery.getPage() - 1) * pageQuery.getPageSize())
				.setMaxResults(pageQuery.getPageSize()).list();
		PageFinder page = new PageFinder(pageQuery.getPage(), pageQuery.getPageSize(), count.intValue(), list);
		return page;
	}else{
		return null;
	}
	
	}
	
	

}
